package cn.hsa.fqz.task.controller;

import cn.hsa.fqz.common.utils.page.PageData;
import cn.hsa.fqz.handler.base.BaseController;
import cn.hsa.fqz.task.controller.vo.SysCodeQueryVO;
import cn.hsa.fqz.task.controller.vo.SysCodeVO;
import cn.hsa.fqz.task.entity.SysCode;
import cn.hsa.fqz.task.entity.SysPageTrackLog;
import cn.hsa.fqz.task.service.SysCodeService;
import cn.hsa.fqz.task.service.SysPageTrackLogService;
import cn.hsa.hsaf.core.framework.context.HsafContextHolder;
import cn.hsa.hsaf.core.framework.util.CurrentUser;
import cn.hsa.hsaf.core.framework.util.PageResult;
import cn.hsa.hsaf.core.framework.web.WrapperResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @ClassName SysController
 * @Author Liu.Jun
 * @Create 2023-07-30 14:08
 * @Version 1.0.0
 */
@Slf4j
@Api(value = "系统管理", tags = "系统管理")
@RestController
@RequestMapping("/system")
public class SysController extends BaseController {

    @Autowired
    private SysPageTrackLogService sysPageTrackLogService;

    @Autowired
    private SysCodeService sysCodeService;


    @ApiOperation(value = "新增页面埋点日志", notes = "新增页面埋点日志")
    @PostMapping("/addTrackLog")
    public WrapperResponse<Integer> list(HttpServletRequest httpServletRequest, @RequestBody SysPageTrackLog sysPageTrackLog) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        sysPageTrackLog.setUserAccount(currentUser.getUserAcct());
        String ip = httpServletRequest.getRemoteAddr();
        sysPageTrackLog.setIpAddr(ip);
        Integer result = sysPageTrackLogService.insertTrackLog(sysPageTrackLog);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "根据分组名称获取字典列表", notes = "根据分组名称获取字典列表")
    @CrossOrigin(value = "*",maxAge = 3600)
    @PostMapping("/code/fields")
    public WrapperResponse<Map<String,List<SysCode>>> selectCodeByField(@RequestBody List<String> fields){
        //List<String> fieldLs = Arrays.asList(fields);
        Map<String,List<SysCode>> result = sysCodeService.getCodeByFields(fields);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "根据分组名称获取字典列表", notes = "根据分组名称获取字典列表")
    @GetMapping("/code/all/fields")
    public WrapperResponse<List<String>> selectAllFields(){
        List<String> result = sysCodeService.selectAllFields();
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "根据子系统编码获取字典列表", notes = "根据子系统编码获取字典列表")
    @GetMapping("/pid/code")
    public WrapperResponse<List<SysCode>> selectCodeByPid(@RequestParam("pid")String pid){
        List<SysCode> result = sysCodeService.getCodeByPid(pid);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "分页查询字典", notes = "分页查询字典")
    @PostMapping("/code/page")
    public WrapperResponse<PageResult<SysCode>> selectSysCodeByPage(@RequestBody SysCodeQueryVO vo){
        PageResult<SysCode> pageResult = sysCodeService.selectSysCodeByPage(vo);
        return WrapperResponse.success(pageResult);
    }

    @ApiOperation(value = "新增字典项", notes = "新增字典项")
    @PostMapping("/code")
    public WrapperResponse<Boolean> insertSysCodeInfo(@RequestBody SysCodeVO vo){
        Boolean result = sysCodeService.insertSysCodeInfo(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "新增字典项", notes = "新增字典项")
    @PutMapping("/code")
    public WrapperResponse<Boolean> updateSysCodeInfo(@RequestBody SysCodeVO vo){
        Boolean result = sysCodeService.updateSysCodeInfo(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "删除字典项", notes = "删除字典项")
    @DeleteMapping("/code")
    public WrapperResponse<Boolean> deleteSysCodeInfo(@RequestParam("id")Integer id){
        Boolean result = sysCodeService.deleteSysCodeInfo(id);
        return WrapperResponse.success(result);
    }
}
